// 生产环境
const path = require('path')
const { merge } = require('webpack-merge') // 合并文件（webpack 5.x 写法）
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const OptimizeCssAssetsWebpackPlugin = require('css-minimizer-webpack-plugin') // 压缩 css

const TerserWebpackPlugin = require('terser-webpack-plugin') // 压缩 js
const common = require('./webpack.common')

module.exports = merge(common, {
  mode: 'production', // 声明生产环境
  output: {
    filename: 'js/[name]-[contenthash:8].bundle.js', // 输出文件
    path: path.join(__dirname, 'release') // 输出文件目录
  },
  plugins: [
    new CleanWebpackPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        // { from: 'public', to: 'public' } // to 可省略, webpack 5.x 写法
        { from: 'src/assets', to: 'assets' },
        {
          from: path.resolve(__dirname, 'public/favicon.ico'),
          to: path.resolve(__dirname, 'release/assets/')
        }
        // {
        //   from: path.resolve(__dirname, 'template'),
        //   to: 'template',
        //   globOptions: { ignore: ['.*'] }
        // }
      ]
    })
  ],
  optimization: {
    // minimize: true, // 压缩输出结果
    minimizer: [ // 配置多个压缩方式
      new OptimizeCssAssetsWebpackPlugin(), // 配置到 minimizer 中，只会当 minimizer 开启时才会使用，建议在这里配置
      new TerserWebpackPlugin()
    ]
  }
})
